#include "NewConnect.h"
#include "ui_NewConnect.h"
#include "DataCtrl.h"
#include <QMenu>
#include <QMessageBox>

#pragma execution_character_set("utf-8")

CNewConnect::CNewConnect(CDataCtrl *pCtrl, QWidget *parent /*= nullptr*/)
	: QDialog(parent)
	, m_ui(new Ui::NewConnect)
	, m_pCtrl(pCtrl)
{
	m_ui->setupUi(this);
	init();
}

CNewConnect::~CNewConnect()
{
	delete m_ui;
}

void CNewConnect::on_btnClose_clicked()
{
	close();
}

void CNewConnect::on_btnSave_clicked()
{
	CConnectOpt opt;
	opt.strName = m_ui->editName->text().trimmed();
	opt.strClientId = m_ui->editClientId->text().trimmed();
	opt.strAddress = m_ui->editServerAddress->text().trimmed();
	opt.port = m_ui->spinServerPort->value();
	opt.strUsername = m_ui->editUsername->text().trimmed();
	opt.strPasswd = m_ui->editPassword->text().trimmed();
	opt.time = QDateTime::currentDateTime();
	opt.strDescribe = m_ui->editDescribe->toPlainText();
	QString strGroupId = m_groupIdIndex.value(m_ui->cboxGroup->currentIndex());
	emit sigInsertConnect(strGroupId, opt);
	close();
}

void CNewConnect::on_btnSaveAndConnect_clicked()
{
	on_btnSave_clicked();
}

void CNewConnect::init()
{
	m_ui->editClientId->setText(QUuid::createUuid().toString());
	const QMap<QString, CGroupOpt>& groups = m_pCtrl->getGroups();
	m_ui->cboxGroup->clear();
	int index = 0;
	for (QMap<QString, CGroupOpt>::ConstIterator it=groups.begin();it!=groups.end();++it)
	{
		m_ui->cboxGroup->insertItem(index, it.value().strName);
		m_groupIdIndex.insert(index, it.key());
		++index;
	}
}
